﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Controls;
using TShooter.TfsWord.Resources;

namespace TShooter.TfsWord.TeamFoundation
{
    public static class WorkItemPickerFactory
    {
        public static Control CreatePicker(Uri teamProjectCollectionUri, string projectName)
        {
            var server = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(teamProjectCollectionUri);

            var pickWorkItemsControl = new PickWorkItemsControl(server.GetService<WorkItemStore>(), false)
                                           {
                                               //AllWorkItemTypesText = Strings.AllWorkItemTypes,
                                               PortfolioDisplayName = projectName,
                                               Dock = DockStyle.Fill,
                                               BannerLabel = Strings.PickWorkItemsControl_BannerLabel,
                                               //PickItemsFromRadioLabel = Strings.PickWorkItemsControl_PickItemsFromRadioLabel,
                                               //PrivateQueriesText = Strings.PickWorkItemsControls_PrivateQueriesText,
                                               //PublicQueriesText = Strings.PickWorkItemsControl_PublicQueriesText,
                                               //QueryRadioLabel = Strings.PickWorkItemsControl_QueryRadioLabel,
                                               //ResetButtonLabel = Strings.PickWorkItemsControl_ResetButtonLabel,
                                               //RunButtonLabel = Strings.PickWorkItemsControl_Find,
                                               //SelectAQueryText = Strings.PickWorkItemsControl_SelectAQueryText,
                                               //SelectListViewLabel = Strings.PickWorkItemsControl_SelectListViewLabel,
                                               //WorkItemTypeLabel = Strings.PickWorkItemsControl_WorkItemTypeLabel
                                           };

            pickWorkItemsControl.ValidateAndApplyControlState();

            return pickWorkItemsControl;
        }

        public static IEnumerable<int> GetSelectedWorkItems(Control ctl)
        {
            var pickWorkItemsControl = (PickWorkItemsControl) ctl;
            var items = pickWorkItemsControl.SelectedWorkItems();

            if (items != null)
            {
                foreach(var wi in items)
                {
                    yield return wi.Id;
                }
            }

            yield break;
        }
    }
}